package com.mall.auth.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mall.auth.constant.MessageConstant;
import com.mall.auth.entity.SecurityUser;
import com.mall.common.entity.UserDTO;
import com.mall.user.api.UserApi;
import com.mall.user.entity.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: com.mall.com.mall.auth.service-> UserServiceImpl
 * @description: 用户、应用管理业务类
 * @author: wuyijie
 * @createDate: 2021/6/8 21:55
 */
@Component
@Slf4j
public class UserServiceImpl implements UserDetailsService {
    @DubboReference
    private UserApi userApi;


    @Autowired
    private HttpServletRequest request;
    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("username={}",username);
//        String clientId = request.getParameter("client_id");
//        UserDto userDto;
//        if(AuthConstant.ADMIN_CLIENT_ID.equals(clientId)){
//            userDto = new UserDto(1L,"client","123456",1,"abc", CollUtil.toList("user"));
//        }else{
//            userDto = new UserDto(1L,"client","123456",1,"abc", CollUtil.toList("user"));
//        }
//        if (userDto==null) {
//            throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
//        }
//        userDto.setClientId(clientId);
//        SecurityUser securityUser = new SecurityUser(userDto);

        UserVO userVO = userApi.queryByUsername(username);
        UserDTO userDTO= BeanUtil.toBean(userVO,UserDTO.class);

        if (ObjectUtil.isEmpty(userVO)) {
            throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        // 用户角色也应在数据库中获取
        String role = "ROLE_ADMIN";
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(role));
        SecurityUser user = new SecurityUser(userDTO);
//        SecurityUser securityUser = new SecurityUser(userVO);
        if (!user.isEnabled()) {
            throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
        } else if (!user.isAccountNonLocked()) {
            throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
        } else if (!user.isAccountNonExpired()) {
            throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
        } else if (!user.isCredentialsNonExpired()) {
            throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
        }
        return user;
    }

}
